package spotify;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created with IntelliJ IDEA.
 * User: Serhat
 * Date: 1-6-13
 * Time: 14:31
 */
public class SpotifyGateway implements ISpotifyGateway {
    private static String baseURL = "http://ws.spotify.com/search/1/track?q=";

    public String doSearch(String searchName){
        String inputSpaces = "";
        for(char c : searchName.toCharArray()) {
            if(c == " ".charAt(0)) inputSpaces += "%20";
            else inputSpaces += c;
        }
        URL url;
        HttpURLConnection conn;
        BufferedReader rd;
        String line;
        String result = "";
        try {
            url = new URL(baseURL + inputSpaces);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = rd.readLine()) != null) {
                result += line;
            }
            rd.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}